package com.hkd.common.data.config.type_handler;

import com.hkd.common.api.enums.AreaType;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author HKD
 * @date 2022/9/11 15:32
 */
@Component
@MappedTypes(AreaType.class)
@MappedJdbcTypes(value = JdbcType.TINYINT, includeNullJdbcType = true)
public class AreaTypeHandler extends BaseTypeHandler<AreaType> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, AreaType areaType, JdbcType jdbcType) throws SQLException {
        if (preparedStatement != null) {
            preparedStatement.setInt(i,areaType.getCode());
        }
    }

    @Override
    public AreaType getNullableResult(ResultSet resultSet, String s) throws SQLException {
        int typeCode = resultSet.getInt(s);
        return AreaType.search(typeCode);
    }

    @Override
    public AreaType getNullableResult(ResultSet resultSet, int i) throws SQLException {
        int typeCode = resultSet.getInt(i);
        return AreaType.search(typeCode);
    }

    @Override
    public AreaType getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        int typeCode = callableStatement.getInt(i);
        return AreaType.search(typeCode);
    }
}
